/**
 * @class
 * @constructor
 */
function Application() {
	
	var genefinder = null;
	
	var flyatlas = null;
	
	var deferredSelection = null;
	
	var that = this;
	
	// called by this.init
	
	this.initGenefinder = function() {
	    try {
    		// initialise genefinder widget
    		flyui.info("instantiate a service for the genefinder widget");
    		var service = new flyui.flybase.Service("../../../data/flybase");
    		
    		flyui.info("instantiate a renderer for the genefinder widget");
    		var renderPane = document.getElementById("genefinderWidget");
    		var renderer = new CustomGeneFinderRenderer(); // custom renderer
    //		var renderer = new flyui.genefinder.DefaultRenderer(); // default renderer
    		renderer.setCanvas(renderPane);
    		
    		flyui.info("instantiate a genefinder widget");
    		genefinder = new flyui.genefinder.Widget(service, renderer);
	    } catch (error) {
	        this.error(error);
	    }
	};
	
	// called by this.init
	
	this.initFlyatlas = function() {
        try {
    		flyui.info("instantiate a service for the flyatlas widget");
    		var service = new flyui.flyatlas.Service();
    		service.setEndpoint("../../../data/flyatlas");
    		
    		flyui.info("instantiate a renderer for the flyatlas widget");
    		var renderer = new flyui.flyatlas.Widget.DefaultRenderer(); 
    		renderer.setCanvas($("#flyatlasWidget"));
    		
    		flyui.info("instantiate a flyatlas widget");
    		flyatlas = new flyui.flyatlas.Widget(service, renderer);
        } catch (error) {
            this.error(error);
        }
	};
	
	// called by this.init
	
	this.initState = function() {
        try {
    		var url = new String(window.location.href);
    		var qi = url.indexOf("#query=");
    		var si = url.indexOf(";selection=");
    		
    		if (qi > 0) {
    			var end = (si > 0) ? si : url.length;
    			var query = url.substring(qi+7,end);
    			flyui.info("found query: "+query);
    			genefinder.findGenesByAnyName(query,false);
    		}
    		
    		if (si > 0) {
    			var selection = url.substring(si+11,url.length);
    			flyui.info("found selection: "+selection);
    			deferredSelection = selection;
    		}
        } catch (error) {
            this.error(error);
        }
	};

	this.init = function() {
        try {
    		
    		var logReader = new YAHOO.widget.LogReader("logger");
    
    		this.initGenefinder();
    		
    		this.initFlyatlas();
    		
    		// attach gene selection listener to genefinder
    		genefinder.subscribe("GENESELECTED", this.onGeneSelected, null);
    		genefinder.subscribe("GENESFOUND", this.onGenesFound, null);
    
    		// attach form submission listener to form
    		flyui.info("hook form submission to widget call");
    		YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
    		
    		flyui.info("application loaded, showing main pane");
    		flyui.mvcutils.hide(document.getElementById("loadingPane"));
    		flyui.mvcutils.show(document.getElementById("applicationPane"));
    		
    		this.initState();
        } catch (error) {
            this.error(error);
        }
	};
	
	this.onGenesFound = function(type, args) {
	    try {
    		flyui.info("application: genes found");
    		var genes = args[0];
    		if (deferredSelection != null) {
    			flyui.info("attempting to apply deferred selection: "+deferredSelection);
    			var index = -1;
    			for (var i=0; i<genes.length; i++) {
    				if (deferredSelection == genes[i].flybaseID) {
    					index = i;
    				}
    			}
    			if (index >= 0) {
    				genefinder.setSelectionIndex(index);
    			}
    			else {
    				flyui.info("failed to apply deferred selection: "+deferredSelection, "error");
    			}
    			deferredSelection = null;
    		}
        } catch (error) {
            this.error(error);
        }
	}
	
	this.onGeneSelected = function(type, args) {
        try {
    		var gene = args[0];
    		flyui.info("gene selected: "+gene+" "+gene.flybaseID);
    		flyatlas.getAssaysByGene(gene);
    		flyui.mvcutils.show(document.getElementById("flyatlasWidget"));
    		that.setSelectionState(gene);
        } catch (error) {
            flyui.debug("caught error on gene selected");
            handleError(error);
        }
	};	
	
	this.onFormSubmitted = function(event) {
	    try {
    		flyui.info("form submitted");
            $("#errorMessage")
               .hide()
               .html("this should never be visible");
    		var query = document.getElementById("queryInput").value;
    		document.getElementById("queryInput").value = "";
    		var caseSensitive = false;
    		flyui.info("case sensitive: "+caseSensitive);
    		genefinder.findGenesByAnyName(query, caseSensitive);
    		flyui.mvcutils.hide(document.getElementById("flyatlasWidget"));
    		that.setQueryState(query);
        } catch (error) {
            handleError(error);
        }
	};
	
	this.setQueryState = function(query) {
	    try {
    		var url = new String(window.location.href);
    		var term = url.indexOf("#");
    		if (term > 0) {
    			url = url.substring(0, term);
    		}
    		window.location.href = url + "#query=" + query;
        } catch (error) {
            handleError(error);
        }
	};
	
	this.setSelectionState = function(gene) {
	    try {
    		var url = new String(window.location.href);
    		var term = url.indexOf(";selection=");
    		if (term > 0) {
    			url = url.substring(0, term);
    		}
    		window.location.href = url + ";selection=" + gene.flybaseID;
        } catch (error) {
            handleError(error);
        }
	}
	
	this.init();
}

function handleError(error) {
    flyui.err(error.name+" :: "+error.message);
    $("#errorMessage")
       .html("an error has occurred, please see the log for more details")
       .show();
}
    

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new Application();});